

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import webpage.CreateDBConnection;

/**
 * Servlet implementation class CodeVerify
 */
@WebServlet("/CodeVerify")
public class CodeVerify extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	ArrayList<String>details = new ArrayList<String>();
	String verifyResult;
    public CodeVerify() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String code = (String)request.getSession().getAttribute("code");
		String loginEmail = (String)request.getSession().getAttribute("loginEmail");
		String enter_code = request.getParameter("enter_code");
		System.out.println(code+enter_code);
		if (code.equals(enter_code)){
			CreateDBConnection create = new CreateDBConnection();
			String result = create.updateColumn("email_valid", "Y", loginEmail);
			if (result.equals("Success")){verifyResult="Email has been successfully verified";}
			
			
			CreateDBConnection getDetailsAgain = new CreateDBConnection();
        	details = getDetailsAgain.getDetails(loginEmail);
        	HttpSession session = request.getSession(true);
        	session.setAttribute("details", details);
			request.setAttribute("verifyResult", verifyResult);
			request.getRequestDispatcher("Profile.jsp").forward(request, response);
		}
		else{
			System.out.println("wrong");
			verifyResult = "Incorrect verification code. Please try again";
			request.setAttribute("verifyResult", verifyResult);
			request.getRequestDispatcher("verify_email.jsp").forward(request, response);
		}
	}

}
